package cn.fl.risk.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.risk.condition.RskTelSignLogQC;
import cn.fl.risk.service.IRskCstSignLogService;
import cn.fl.risk.service.IRskTelSignLogService;
import cn.fl.risk.vo.RskTelSignLogVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 手机号码风险标记记录表 控制层
 */
@Controller
@RequestMapping("/risk/rskTelSignLog")
public class RskTelSignLogController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(RskTelSignLogController.class);

	/**
	 * RskTelSignLog 服务层BEAN
	 */
	@Autowired
	private IRskTelSignLogService rskTelSignLogService;

	@Autowired
	private IRskCstSignLogService rskCstSignLogService;


	/**
	 * 加载手机号码风险标记页面
	 */
	@RequestMapping(value = "/toTelListPage.do", method = { RequestMethod.GET})
	public ModelAndView toVINListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toVINListPage", "加载手机号码风险标记页面.", CurrentThreadContext.getCurrentUserName());
		ModelAndView mav = new ModelAndView("risk/rskTelSignLog/rskTelSignLogList");
		return mav;
	}

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String tel = request.getParameter("tel");
		ModelAndView view = new ModelAndView("risk/rskTelSignLog/rskTelSignLogDetailList");
		view.addObject("tel", tel);
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		List<KeyValueVO> signResTypeList = this.rskCstSignLogService.searchCdList(DictionaryConstant.CODE_GRP_NM_SIGN_RES_TYPE);
		List<KeyValueVO> isValidList = new ArrayList<>();
		KeyValueVO keyValueVO1 = new KeyValueVO();
		keyValueVO1.setKey("1");
		keyValueVO1.setValue("有效");
		isValidList.add(keyValueVO1);
		KeyValueVO keyValueVO2 = new KeyValueVO();
		keyValueVO2.setKey("0");
		keyValueVO2.setValue("无效");
		isValidList.add(keyValueVO2);
		ModelAndView view = new ModelAndView("risk/rskTelSignLog/rskTelSignLogAdd");
		view.addObject("signResTypeList", JSON.toJSONString(signResTypeList));
		view.addObject("isValidList", JSON.toJSONString(isValidList));
		view.addObject("currentUserName", CurrentThreadContext.getCurrentUserName());
		view.addObject("crtTm", DateUtils.formatDateToString(new Date(),DateUtils.YYYY_MM_DDHHMMSS));

		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toModifyPage", "加载修改页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String tel = request.getParameter("tel");
		ModelAndView view = new ModelAndView("risk/rskTelSignLog/rskTelSignLogModify");

		if(StringUtils.isNotEmpty(tel)){
			RskTelSignLogQC rskTelSignLogQC = new RskTelSignLogQC();
			rskTelSignLogQC.setTel(tel);
			rskTelSignLogQC.setIsDel(BaseConstant.IS_YESNO_NO);
			rskTelSignLogQC.setIsNew(BaseConstant.IS_YESNO_YES);
			List<RskTelSignLogVO> rskTelSignLogVOS = this.rskTelSignLogService.searchList(rskTelSignLogQC);
			if(CollectionUtils.isNotEmpty(rskTelSignLogVOS)){
				RskTelSignLogVO rskTelSignLogVO = rskTelSignLogVOS.get(0);
				rskTelSignLogVO.setCrtTm(DateUtils.formatDate(rskTelSignLogVO.getCrtTm(),DateUtils.YYYY_MM_DDHHMMSS));
				view.addObject("crtTm", DateUtils.formatDateToString(rskTelSignLogVO.getCrtTm(),DateUtils.YYYY_MM_DDHHMMSS));
				view.addObject("rskTelSignLogVO", rskTelSignLogVO);
			}
		}
		// 查询数据库字典，查询客户风险标记类型
		List<KeyValueVO> signResTypeList = this.rskCstSignLogService.searchCdList(DictionaryConstant.CODE_GRP_NM_SIGN_RES_TYPE);
		List<KeyValueVO> isValidList = new ArrayList<>();
		KeyValueVO keyValueVO1 = new KeyValueVO();
		keyValueVO1.setKey("1");
		keyValueVO1.setValue("有效");
		isValidList.add(keyValueVO1);
		KeyValueVO keyValueVO2 = new KeyValueVO();
		keyValueVO2.setKey("0");
		keyValueVO2.setValue("无效");
		isValidList.add(keyValueVO2);

		view.addObject("signResTypeList", JSON.toJSONString(signResTypeList));
		view.addObject("isValidList", JSON.toJSONString(isValidList));
		view.addObject("currentUserName", CurrentThreadContext.getCurrentUserName());
		view.addObject("mdfTm", DateUtils.formatDateToString(new Date(),DateUtils.YYYY_MM_DDHHMMSS));
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toModifyPage", "加载修改.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", "加载例子详细页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("lnkcomp/rskTelSignLog/rskTelSignLogDetail");
		view.addObject("id", id);
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", " 加载例子详细页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String isNew = request.getParameter("isNew");
		String tel = request.getParameter("tel");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		RskTelSignLogQC qc = new RskTelSignLogQC();
		if (StringUtils.isNotEmpty(isNew)) {
			qc.setIsNew(Integer.valueOf(isNew));
		}
		if (StringUtils.isNotEmpty(tel)) {
			qc.setTel(tel);
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = this.rskTelSignLogService.searchListPage(page);
		List<RskTelSignLogVO> list = (List<RskTelSignLogVO>) pager.getResultList();
		ListPageVO<RskTelSignLogVO> listPage = new ListPageVO<RskTelSignLogVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String tel = request.getParameter("tel");
		String signResType = request.getParameter("signResType");
		String signRes = request.getParameter("signRes");
		String signRem = request.getParameter("signRem");
		String isValid = request.getParameter("isValid");

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		if(StringUtils.isEmpty(tel)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("手机号码为空!");
			BaseLogUtils.info(logger, "modify", "手机号码为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		RskTelSignLogQC rskTelSignLogQC = new RskTelSignLogQC();
		rskTelSignLogQC.setTel(tel);
		rskTelSignLogQC.setIsDel(BaseConstant.IS_YESNO_NO);
		rskTelSignLogQC.setIsNew(BaseConstant.IS_YESNO_YES);
		List<RskTelSignLogVO> rskTelSignLogVOS = this.rskTelSignLogService.searchList(rskTelSignLogQC);
		if(CollectionUtils.isNotEmpty(rskTelSignLogVOS)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("该手机号已标记!");
			BaseLogUtils.info(logger, "modify", "该手机号已标记!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		if(StringUtils.isEmpty(signResType)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("标记类型为空!");
			BaseLogUtils.info(logger, "modify", "标记类型为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		if(StringUtils.isEmpty(isValid)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("是否有效为空!");
			BaseLogUtils.info(logger, "modify", "是否有效为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		RskTelSignLogVO entity = null;
		try {

			entity = new RskTelSignLogVO();
			entity.setTel(tel);
			entity.setSignResType(signResType);
			entity.setIsValid(Integer.valueOf(isValid));
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			entity.setIsNew(BaseConstant.IS_YESNO_YES);
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setCrtUsrNm(CurrentThreadContext.getCurrentUserName());
			if(StringUtils.isNotEmpty(signRem)){
				entity.setSignRem(signRem);
			}
			if(StringUtils.isNotEmpty(signRes)){
				entity.setSignRes(signRes);
			}
			//TODO ADD PARAM
			Long id = this.rskTelSignLogService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增对象失败,entity["+entity+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "add", "新增对象失败,entity["+entity+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String tel = request.getParameter("tel");
		String signResType = request.getParameter("signResType");
		String signRes = request.getParameter("signRes");
		String signRem = request.getParameter("signRem");
		String isValid = request.getParameter("isValid");

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(tel)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("手机号码为空!");
			BaseLogUtils.info(logger, "modify", "手机号码为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		if(StringUtils.isEmpty(signResType)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("标记类型为空!");
			BaseLogUtils.info(logger, "modify", "标记类型为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		if(StringUtils.isEmpty(isValid)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("是否有效为空!");
			BaseLogUtils.info(logger, "modify", "是否有效为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		RskTelSignLogVO entity = null;
		try {
			entity = new RskTelSignLogVO();
			entity.setTel(tel);
			entity.setSignResType(signResType);
			entity.setIsValid(Integer.valueOf(isValid));
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			entity.setIsNew(BaseConstant.IS_YESNO_YES);
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setMdfUsrNm(CurrentThreadContext.getCurrentUserName());
			if(StringUtils.isNotEmpty(signRem)){
				entity.setSignRem(signRem);
			}
			if(StringUtils.isNotEmpty(signRes)){
				entity.setSignRes(signRes);
			}
			boolean success = this.rskTelSignLogService.modify(entity);
			BaseLogUtils.info(logger, "modify", "修改成功.[tel="+tel+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改对象失败,tel["+tel+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "修改对象失败,tel["+tel+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.rskTelSignLogService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			RskTelSignLogVO vo = this.rskTelSignLogService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}
}

